package cn.com.bluemoon.daps.standard.api.factory;

import cn.com.bluemoon.daps.common.constant.ServiceNameConstants;
import cn.com.bluemoon.daps.common.constant.ServiceNameConstants.DapsApiStandard;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.FeignClientTools;
import cn.com.bluemoon.daps.standard.api.RemoteSystemCategoryService;
import cn.com.bluemoon.daps.standard.entity.DapSystemCategory;
import cn.com.bluemoon.daps.standard.vo.SysTopicCategorySimpleVo;
import cn.com.bluemoon.daps.standard.vo.SystemCategoryTreeVo;
import cn.com.bluemoon.daps.standard.vo.TopicCategoryVo;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 标准服务降级处理
 *
 * @author Jarod Kong
 */
@Component
public class RemoteCategoryFallbackFactory implements FallbackFactory<RemoteSystemCategoryService> {
    private static final Logger log = LoggerFactory.getLogger(RemoteCategoryFallbackFactory.class);

    @Override
    public RemoteSystemCategoryService create(Throwable throwable) {
        log.error("数据标准-分类调用失败:{}", throwable.getMessage());
        return new RemoteSystemCategoryService() {

            @Override
            public ResultBean<List<DapSystemCategory>> getSystemCategories(List<String> ids) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiStandard.PROD_SERVICE, "获取主题分类失败");
            }

            @Override
            public ResultBean<Map<String, List<DapSystemCategory>>> sysCategories() {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiStandard.PROD_SERVICE, "获取主题分类失败");
            }

            @Override
            public ResultBean<TopicCategoryVo> getById(String cId) {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, DapsApiStandard.PROD_SERVICE, "获取主题分类失败");
            }

            @Override
            public ResultBean<List<SystemCategoryTreeVo>> categoryTree() {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiStandard.PROD_SERVICE, "获取主题分类树列表失败");
            }

            @Override
            public ResultBean<List<SysTopicCategorySimpleVo>> sysTopicCategories() {
                return FeignClientTools.INSTANCE.errorMsgCatch(throwable, ServiceNameConstants.DapsApiStandard.PROD_SERVICE, "获取主题分类失败");
            }
        };
    }
}
